﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Model;
using Public;

namespace Dal
{
    public class Event
    {
        /// <summary>
        /// 问题回答录入
        /// </summary>
        public class DALSuunto_Answer
        {
            /// <summary>
            /// 记录问题回答
            /// </summary>
            /// <param name="con">数据库链接</param>
            /// <param name="module">信息实体</param>
            /// <returns>是否执行成功</returns>
            public bool AnswerInsert(string con, ModelEvent.ModelSuunto_Answer module)
            {
                bool flag = false;
                SqlParameter[] parameter ={
                                          new SqlParameter("@uid",SqlDbType.NVarChar,20),
                                          new SqlParameter("@question",SqlDbType.NVarChar,500),
                                          new SqlParameter("@answer",SqlDbType.NVarChar,500),
                                          new SqlParameter("@createtime",SqlDbType.DateTime)
                                      };
                parameter[0].Value = module.UID;
                parameter[1].Value = module.Question;
                parameter[2].Value = module.Answer;
                parameter[3].Value = DateTime.Now;
                try
                {
                    int i = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(con), CommandType.StoredProcedure, "ProAnswerInsert", parameter);
                    if (i > 0) flag = true;
                }
                catch (Exception e)
                { }
                return flag;

            }
        }

        /// <summary>
        /// 获取城市信息
        /// </summary>
        public class DALSuunto_CityInfo
        {
            /// <summary>
            /// 获取城市信息
            /// </summary>
            /// <param name="con">数据库链接</param>
            /// <param name="module">信息实体</param>
            /// <returns>信息</returns>
            public DataTable GetCityList(string con, ModelEvent.ModelSuunto_CityInfo module)
            {
                SqlParameter[] parameter ={
                                         new SqlParameter("@parentone",SqlDbType.NVarChar,50),
                                         new SqlParameter("@parenttwo",SqlDbType.NVarChar,50),
                                         new SqlParameter("@tier",SqlDbType.Int)
                                     };
                parameter[0].Value = module.ParentOne;
                parameter[1].Value = module.ParentTwo;
                parameter[2].Value = module.Tier;
                DataTable dt = new DataTable();
                try
                {
                    dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(con), CommandType.StoredProcedure, "ProGetCityInfo", parameter);
                }
                catch (Exception ex)
                {
                    dt = null;
                }
                return dt;
            }
        }

        /// <summary>
        /// 订单处理
        /// </summary>
        public class DALSuunto_Order
        {
            /// <summary>
            /// 新增表单信息
            /// </summary>
            /// <param name="con">数据库链接</param>
            /// <param name="module">信息实体</param>
            /// <returns>是否执行成功</returns>
            public bool SuuntoOrderInsert(string con, ModelEvent.ModelSuunto_Order module)
            {
                bool flag = false;
                SqlParameter[] parameter ={
                                         new SqlParameter("@pid",SqlDbType.Int),
                                         new SqlParameter("@uid",SqlDbType.NVarChar,20),
                                         new SqlParameter("@count",SqlDbType.Int),
                                         new SqlParameter("@money",SqlDbType.Money),
                                         new SqlParameter("@keycode",SqlDbType.NVarChar,11),
                                         new SqlParameter("@takeaddress",SqlDbType.Int), 
                                         new SqlParameter("@createtime",SqlDbType.DateTime),
                                         new SqlParameter("@pkid",SqlDbType.NVarChar,20),
                                         new SqlParameter("@state",SqlDbType.NVarChar,50) 
                                     };
                parameter[0].Value = module.PID;
                parameter[1].Value = module.UID;
                parameter[2].Value = module.Count;
                parameter[3].Value = module.Money;
                parameter[4].Value = module.KeyCode;
                parameter[5].Value = module.TakeAddress;
                parameter[6].Value = DateTime.Now;
                parameter[7].Value = module.PKID;
                parameter[8].Value = module.State;
                try
                {
                    int i = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(con), CommandType.StoredProcedure, "[ProOrderInsert]", parameter);
                    if (i > 0) flag = true;
                }
                catch (Exception ex)
                {
                }
                return flag;
            }
            /// <summary>
            /// 更新订单信息
            /// </summary>
            /// <param name="con">数据库链接</param>
            /// <param name="module">信息实体</param>
            /// <returns>是否执行成功</returns>
            public bool SuuntoOrderUpdate(string con, ModelEvent.ModelSuunto_Order module)
            {
                bool flag = false;
                SqlParameter[] parameter ={
                                         new SqlParameter("@pkid",SqlDbType.NVarChar,20),
                                         new SqlParameter("@updatetime",SqlDbType.DateTime),
                                         new SqlParameter("@state",SqlDbType.NVarChar,50),
                                         new SqlParameter("@update",SqlDbType.Char,1) 
                                     };
                parameter[0].Value = module.PKID;
                parameter[1].Value = DateTime.Now;
                parameter[2].Value = module.State;
                parameter[3].Value = "F";
                try
                {
                    int i = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(con), CommandType.StoredProcedure, "[ProOrderUpdate]", parameter);
                    if (i > 0) flag = true;
                }
                catch (Exception ex)
                { }
                return flag;
            }
            /// <summary>
            /// 生成订单编号
            /// </summary>
            /// <param name="con"></param>
            /// <returns></returns>
            public string GetOrderID(string con)
            {
                string returnStr = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                string userCount = string.Empty;
                try
                {
                    userCount = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(con), CommandType.StoredProcedure, "[ProOrderGetCount]").Rows[0][0].ToString();
                    returnStr = returnStr + string.Format("{0:D3}", Convert.ToInt32(userCount));
                }
                catch (Exception)
                {
                    returnStr = returnStr + "001";
                }
                return returnStr;
            }
            /// <summary>
            /// 更新订单
            /// </summary>
            /// <param name="con">数据库链接</param>
            /// <param name="orderId">订单编号</param>
            /// <param name="state">订单状态</param>
            /// <param name="datetime">修改时间</param>
            /// <returns>是否修改成功</returns>
            public bool SuuntoOrderUpdate(string con,string orderId,string state,DateTime datetime)
            {
                SqlParameter[] parameter = {
                                               new SqlParameter("@orderid", SqlDbType.NVarChar, 20),
                                               new SqlParameter("@state", SqlDbType.NVarChar, 50),
                                               new SqlParameter("@datetime", SqlDbType.DateTime)
                                           };
                parameter[0].Value = orderId;
                parameter[1].Value = state;
                parameter[2].Value = datetime;
                bool flag = false;
                try
                {
                    int i = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(con), CommandType.StoredProcedure,
                                                      "ProOrderUpdateByID", parameter);
                    if (i > 0) flag = true;
                }
                catch (Exception)
                {
                }
                return flag;
            }
            /// <summary>
            /// 获取订单信息
            /// </summary>
            /// <param name="con">数据库链接</param>
            /// <param name="pkid">订单编号</param>
            /// <returns>订单信息</returns>
            public DataTable SuuntoOrderByPKID(string con,string pkid)
            {
                SqlParameter parameter = new SqlParameter("@pkid", SqlDbType.NVarChar, 20);
                parameter.Value = pkid;
                DataTable dt=new DataTable();
                try
                {
                    dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(con), CommandType.StoredProcedure, "ProOrderByID", parameter);
                }
                catch (Exception)
                {
                    dt = null;
                }
                return dt;
            }
        }

        /// <summary>
        /// 支付订单记录
        /// </summary>
        public class DALSuunto_Paylog
        {
            /// <summary>
            /// 保存支付日志
            /// </summary>
            /// <param name="con">数据库链接</param>
            /// <param name="model">信息实体</param>
            public void SuuntoPaylogInsert(string con, ModelEvent.ModelSuunto_Paylog model)
            {
                SqlParameter[] parameters = {
                                                new SqlParameter("@buyer_email", SqlDbType.NVarChar, 500),
                                                new SqlParameter("@buyer_id", SqlDbType.NVarChar, 20),
                                                new SqlParameter("@discount", SqlDbType.NVarChar, 10),
                                                new SqlParameter("@gmt_create", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@gmt_logistics_modify", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@gmt_payment", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@is_success", SqlDbType.NVarChar, 10),
                                                new SqlParameter("@is_total_fee_adjust", SqlDbType.NVarChar, 10),
                                                new SqlParameter("@logistics_fee", SqlDbType.NVarChar, 10),
                                                new SqlParameter("@logistics_payment", SqlDbType.NVarChar, 500),
                                                new SqlParameter("@logistics_type", SqlDbType.NVarChar, 500),
                                                new SqlParameter("@notify_id", SqlDbType.NVarChar, 500),
                                                new SqlParameter("@notify_time", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@notify_type", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@out_trade_no", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@payment_type", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@price", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@quantity", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@receive_address", SqlDbType.NVarChar,50),
                                                new SqlParameter("@receive_mobile", SqlDbType.NVarChar, 20),
                                                new SqlParameter("@receive_name", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@receive_zip", SqlDbType.NVarChar, 10),
                                                new SqlParameter("@seller_actions", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@seller_email", SqlDbType.NVarChar, 500),
                                                new SqlParameter("@seller_id", SqlDbType.NVarChar, 20),
                                                new SqlParameter("@sign", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@sign_type", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@subject", SqlDbType.NVarChar, 500),
                                                new SqlParameter("@total_fee", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@trade_no", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@trade_status", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@use_coupon", SqlDbType.NVarChar, 50),
                                                new SqlParameter("@request_page",SqlDbType.NVarChar,50),
                                                new SqlParameter("@body",SqlDbType.NVarChar,500),
                                                new SqlParameter("@description",SqlDbType.NVarChar,4000),
                                                new SqlParameter("@logcreatetime",SqlDbType.DateTime)
                                            };
                parameters[0].Value = model.buyer_email;
                parameters[1].Value = model.buyer_id;
                parameters[2].Value = model.discount;
                parameters[3].Value = model.gmt_create;
                parameters[4].Value = model.gmt_logistics_modify;
                parameters[5].Value = model.gmt_payment;
                parameters[6].Value = model.is_success;
                parameters[7].Value = model.is_total_fee_adjust;
                parameters[8].Value = model.logistics_fee;
                parameters[9].Value = model.logistics_payment;
                parameters[10].Value = model.logistics_type;
                parameters[11].Value = model.notify_id;
                parameters[12].Value = model.notify_time;
                parameters[13].Value = model.notify_type;
                parameters[14].Value = model.out_trade_no;
                parameters[15].Value = model.payment_type;
                parameters[16].Value = model.price;
                parameters[17].Value = model.quantity;
                parameters[18].Value = model.receive_address;
                parameters[19].Value = model.receive_mobile;
                parameters[20].Value = model.receive_name;
                parameters[21].Value = model.receive_zip;
                parameters[22].Value = model.seller_actions;
                parameters[23].Value = model.seller_email;
                parameters[24].Value = model.seller_id;
                parameters[25].Value = model.sign;
                parameters[26].Value = model.sign_type;
                parameters[27].Value = model.subject;
                parameters[28].Value = model.total_fee;
                parameters[29].Value = model.trade_no;
                parameters[30].Value = model.trade_status;
                parameters[31].Value = model.use_coupon;
                parameters[32].Value = model.request_page;
                parameters[33].Value = model.Body;
                parameters[34].Value = model.Description;
                parameters[35].Value = DateTime.Now;
                SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(con), CommandType.StoredProcedure,
                                          "ProPaylogInsert", parameters);

            }
        }

        /// <summary>
        /// 商品信息
        /// </summary>
        public class DALSuunto_Products
        {
            /// <summary>
            /// 添加商品信息
            /// </summary>
            /// <param name="con">数据库链接</param>
            /// <param name="module">数据实体</param>
            /// <returns>是否执行成功</returns>
            public bool SuuntoProductsInsert(string con, ModelEvent.ModelSuunto_Products module)
            {
                bool flag = false;
                SqlParameter[] parameter ={
                                         new SqlParameter("@type",SqlDbType.NVarChar,200),
                                         new SqlParameter("@name",SqlDbType.NVarChar,200),
                                         new SqlParameter("@count",SqlDbType.Int),
                                         new SqlParameter("@price",SqlDbType.Float),
                                         new SqlParameter("@description",SqlDbType.NVarChar,4000),
                                         new SqlParameter("@imgurl",SqlDbType.NVarChar,500),
                                         new SqlParameter("@enname",SqlDbType.NVarChar,50)
                                     };
                parameter[0].Value = module.Type;
                parameter[1].Value = module.Name;
                parameter[2].Value = module.Count;
                parameter[3].Value = module.Price;
                parameter[4].Value = module.Description;
                parameter[5].Value = module.ImgUrl;
                parameter[6].Value = module.EnName;
                try
                {
                    int i = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(con), CommandType.StoredProcedure, "[ProProductsInsert]", parameter);
                    if (i > 0) flag = true;
                }
                catch (Exception ex)
                { }
                return flag;
            }
            /// <summary>
            /// 更新商品信息
            /// </summary>
            /// <param name="con">数据库链接</param>
            /// <param name="module">信息实体</param>
            /// <returns>是否执行成功</returns>
            public bool SuuntoProductsUpdate(string con, ModelEvent.ModelSuunto_Products module)
            {
                bool flag = false;
                SqlParameter[] parameter ={
                                         new SqlParameter("@pkid",SqlDbType.Int)
                                     };
                parameter[0].Value = module.PKID;
                try
                {
                    int i = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(con), CommandType.StoredProcedure, "[ProProductsUpdate]", parameter);
                    if (i > 0) flag = true;
                }
                catch (Exception ex)
                { }
                return flag;

            }
            /// <summary>
            /// 获取商品列表
            /// </summary>
            /// <param name="con"></param>
            /// <returns></returns>
            public DataTable SuuntoProductsList(string con)
            {
                DataTable dt = new DataTable();
                try
                {
                    dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(con), CommandType.StoredProcedure, "ProProductsList");
                }
                catch (Exception ex)
                { dt = null; }
                return dt;
            }
            /// <summary>
            /// 获取商品信息
            /// </summary>
            /// <param name="con">数据库链接</param>
            /// <param name="pkid">商品编号</param>
            /// <returns>商品信息</returns>
            public DataTable SuuntoProductInfo(string con, int pkid)
            {
                DataTable dt = new DataTable();
                SqlParameter[] parameter = { new SqlParameter("@pkid", SqlDbType.Int) };
                parameter[0].Value = pkid;
                try
                {
                    dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(con), CommandType.StoredProcedure, "ProProductsByID", parameter);
                }
                catch (Exception ex)
                { dt = null; }
                return dt;
            }
            /// <summary>
            /// 商品库存查询
            /// </summary>
            /// <param name="con">数据库链接</param>
            /// <param name="pkid">商品编号</param>
            /// <returns>库存是否可用</returns>
            public bool SuuntoProductsCount(string con, int pkid)
            {
                bool flag = false;
                SqlParameter[] parameter = { new SqlParameter("@pkid", SqlDbType.Int) };
                parameter[0].Value = pkid;
                try
                {
                    int i = (Int32)SqlHelper.ExecuteDataTable(DataProtector.DecryptString(con), CommandType.StoredProcedure, "ProProductsCount", parameter).Rows[0][0];
                    if (i > 0) flag = true;
                }
                catch (Exception ex)
                { }
                return flag;
            }
        }

        /// <summary>
        /// 用户信息
        /// </summary>
        public class DALSuunto_Users
        {
            /// <summary>
            /// 添加用户信息
            /// </summary>
            /// <param name="con">数据库链接</param>
            /// <param name="module">信息实体</param>
            /// <returns>用户编号</returns>
            public string SuuntoUsersInsert(string con, ModelEvent.ModelSuunto_Users module)
            {
                string returnStr = string.Empty;
                SqlParameter[] parameter ={
                                         new SqlParameter("@pkid",SqlDbType.NVarChar,20),
                                         new SqlParameter("@username",SqlDbType.NVarChar,50),
                                         new SqlParameter("@gender",SqlDbType.Char,2),
                                         new SqlParameter("@age",SqlDbType.NVarChar,20),
                                         new SqlParameter("@profession",SqlDbType.NVarChar,200),
                                         new SqlParameter("@address",SqlDbType.NVarChar,500),
                                         new SqlParameter("@email",SqlDbType.NVarChar,500),
                                         new SqlParameter("@phone",SqlDbType.NVarChar,20),
                                         new SqlParameter("@createtime",SqlDbType.DateTime),
                                         new SqlParameter("@zipcode",SqlDbType.NVarChar,10) 
                                     };
                parameter[0].Value = module.PKID;
                parameter[1].Value = module.Username;
                parameter[2].Value = module.Gender;
                parameter[3].Value = module.Age;
                parameter[4].Value = module.Profession;
                parameter[5].Value = module.Address;
                parameter[6].Value = module.Email;
                parameter[7].Value = module.Phone;
                parameter[8].Value = DateTime.Now;
                parameter[9].Value = module.ZipCode;
                try
                {
                    int i = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(con), CommandType.StoredProcedure, "[ProUsersInsert]", parameter);
                    if (i > 0) returnStr = module.PKID;
                }
                catch (Exception ex)
                { }
                return returnStr;
            }
            /// <summary>
            /// 生成用户编号
            /// </summary>
            /// <param name="con"></param>
            /// <returns></returns>
            public string GetUserID(string con)
            {
                string returnStr = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                string userCount = string.Empty;
                try
                {
                    userCount = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(con), CommandType.StoredProcedure, "ProUsersGetCount").Rows[0][0].ToString();
                    returnStr = returnStr + string.Format("{0:D3}", Convert.ToInt32(userCount));
                }
                catch (Exception)
                {
                    returnStr = returnStr + "001";
                }
                return returnStr;
            }
            /// <summary>
            /// 获取用户信息
            /// </summary>
            /// <param name="con">数据库链接</param>
            /// <param name="pkid">用户编号</param>
            /// <returns>用户信息</returns>
            public DataTable GetUserInfo(string con,string pkid)
            {
                SqlParameter[] parameters = {new SqlParameter("@pkid", SqlDbType.NVarChar, 20)};
                parameters[0].Value = pkid;
                DataTable dt=new DataTable();
                try
                {
                    dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(con), CommandType.StoredProcedure, "ProUsersByID", parameters);
                }
                catch (Exception)
                {
                    dt = null;
                }
                return dt;
            }
        }

        /// <summary>
        /// 商店信息
        /// </summary>
        public class DALSuunto_Store
        {
            /// <summary>
            /// 获取商店列表信息
            /// </summary>
            /// <param name="con">数据库链接</param>
            /// <returns>列表</returns>
            public DataTable SuuntoStoreList(string con)
            {
                DataTable dt = new DataTable();
                try
                {
                    dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(con), CommandType.StoredProcedure, "ProStoreList");

                }
                catch (Exception ex)
                { dt = null; }
                return dt;
            }
            /// <summary>
            /// 获取商品信息
            /// </summary>
            /// <param name="con">数据库链接</param>
            /// <param name="pkid">商品编号</param>
            /// <returns>商品信息</returns>
            public DataTable SuuntoStoreInfo(string con, int pkid)
            {
                DataTable dt = new DataTable();
                SqlParameter[] parameter = { new SqlParameter("@pkid", SqlDbType.Int) };
                parameter[0].Value = pkid;
                try
                {
                    dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(con), CommandType.StoredProcedure, "ProStoreByID", parameter);
                }
                catch (Exception ex)
                { dt = null; }
                return dt;
            }
        }

        public class  DALSuunto_Email
        {
            public DataTable GetEmailConfig(string con)
            {
                return SqlHelper.ExecuteDataTable(DataProtector.DecryptString(con), CommandType.StoredProcedure,
                                                  "GetMailConfigInfo");
            }
        }

        public class DALTAccountForm
        {
            public int GetMaxID(string sqlCon)
            {
                DataTable dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(sqlCon), CommandType.StoredProcedure, "ProTAccountFormMaxID");
                return int.Parse(dt.Rows[0][0].ToString());
            }

            public bool TAccountFormAdd(string sqlCon, Model.TAccountForm models)
            {
                try
                {
                    SqlParameter[] parameter ={new SqlParameter("@PKID",SqlDbType.Int), 
                                           new SqlParameter("@UserName",SqlDbType.NVarChar,50),
                                           new SqlParameter("@Email",SqlDbType.NVarChar,500),
                                           new SqlParameter("@Phone",SqlDbType.NVarChar,50),
                                           new SqlParameter("@IPAddress",SqlDbType.NVarChar,15),
                                           new SqlParameter("@SerialNumber",SqlDbType.NVarChar,50),
                                           new SqlParameter("@StoreID",SqlDbType.Int),
                                           new SqlParameter("@CreateTime",SqlDbType.DateTime),
                                           new SqlParameter("@CheckState",SqlDbType.Bit),
                                           new SqlParameter("@CheckMessage",SqlDbType.NVarChar,1000),
                                           new SqlParameter("@Model",SqlDbType.NVarChar,200)};
                    parameter[0].Value = models.PKID;
                    parameter[1].Value = models.UserName;
                    parameter[2].Value = models.Email;
                    parameter[3].Value = models.Phone;
                    parameter[4].Value = models.IPAddress;
                    parameter[5].Value = models.SerialNumber;
                    parameter[6].Value = models.StoreID;
                    parameter[7].Value = models.CreateTime;
                    parameter[8].Value = models.CheckState;
                    parameter[9].Value = models.CheckMessage;
                    parameter[10].Value = models.Model;

                   int i=SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(sqlCon), CommandType.StoredProcedure, "ProTAccountFormInsert", parameter);
                    if (i>0) return true; else return false;
                }
                catch (Exception)
                {
                    return false;
                }
            }

            public bool TAccountFormUpdate(string sqlCon, Model.TAccountForm models)
            {
                try
                {
                    SqlParameter[] parameter ={new SqlParameter("@PKID",SqlDbType.Int), 
                                           new SqlParameter("@UpdateTime",SqlDbType.DateTime),
                                           new SqlParameter("@CheckState",SqlDbType.Bit),
                                           new SqlParameter("@CheckMessage",SqlDbType.NVarChar,1000)};
                    parameter[0].Value = models.PKID;
                    parameter[1].Value = models.UpdateTime;
                    parameter[2].Value = models.CheckState;
                    parameter[3].Value = models.CheckMessage;

                    int i = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(sqlCon), CommandType.StoredProcedure, "ProTAccountFormUpdate", parameter);
                    if (i > 0) return true; else return false;
                }
                catch (Exception)
                {
                    return false;
                }
            }

        }
        public class DALTCheckLog
        {
            public bool TCheckLogAdd(string sqlCon, Model.TCheckLog models)
            {
                try
                {
                    SqlParameter[] parameter ={
                                                 new SqlParameter("@UserID",SqlDbType.Int),
                                                 new SqlParameter("@SerialNumber",SqlDbType.NVarChar,50),
                                                 new SqlParameter("@Phone",SqlDbType.NVarChar,50),
                                                 new SqlParameter("@CheckTime",SqlDbType.DateTime),
                                                 new SqlParameter("@CheckState",SqlDbType.Bit),
                                                 new SqlParameter("@CheckMessage",SqlDbType.NVarChar,1000)
                                             };
                    parameter[0].Value = models.UserID;
                    parameter[1].Value = models.SerialNumber;
                    parameter[2].Value = models.Phone;
                    parameter[3].Value = models.CheckTime;
                    parameter[4].Value = models.CheckState;
                    parameter[5].Value = models.CheckMessage;
                    int i = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(sqlCon), CommandType.StoredProcedure, "ProTCheckLogInsert", parameter);
                    if (i > 0) return true; else return false;
                }
                catch (Exception) { return false; }
            }

            public bool TCheckLogExistsByPhone(string sqlCon, string phone,DateTime beginTime)
            {
                try
                {
                    SqlParameter[] parameter = { new SqlParameter("@Phone", SqlDbType.NVarChar, 50),
                                                 new SqlParameter("@BeginTime",SqlDbType.DateTime)
                                               };
                    parameter[0].Value = phone;
                    parameter[1].Value = beginTime;
                    DataTable dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(sqlCon), CommandType.StoredProcedure, "ProTCheckLogExistsByPhone", parameter);
                    if (int.Parse(dt.Rows[0][0].ToString()) >= 2) return false; else return true;
                }
                catch (Exception)
                { return false; }
            }

            public bool TCheckLogExistsByUserID(string sqlCon, int userId)
            {
                try
                {
                    SqlParameter[] parameter = { new SqlParameter("@UserID", SqlDbType.Int) };
                    parameter[0].Value = userId;
                    DataTable dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(sqlCon), CommandType.StoredProcedure, "ProTCheckLogExistsByUserID", parameter);
                    if (int.Parse(dt.Rows[0][0].ToString()) >= 2) return false; else return true;
                }
                catch (Exception)
                { return false; }
            }
        }
        public class DALTStore
        {
            public List<Model.TStore> TStoreCity(string sqlCon)
            {
                try
                {
                    DataTable dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(sqlCon), CommandType.StoredProcedure, "ProTStoreSelectCity");
                    if (dt != null)
                    {
                        var returnList = new List<Model.TStore>();
                        foreach (DataRow dr in dt.Rows)
                        {
                            returnList.Add(new Model.TStore() { City = dr[0].ToString() });
                        }
                        return returnList;
                    }
                    else { return null; }
                }
                catch (Exception) { return null; }

            }

            public List<Model.TStore> TStoreName(string sqlCon, string city)
            {
                try
                {
                    SqlParameter[] parameter = { new SqlParameter("@City", SqlDbType.NVarChar, 50) };
                    parameter[0].Value = city;

                    DataTable dt= SqlHelper.ExecuteDataTable(DataProtector.DecryptString(sqlCon), CommandType.StoredProcedure, "ProTStoreSelectName", parameter);
                    if (dt != null)
                    {
                        var returnList = new List<Model.TStore>();
                        foreach (DataRow dr in dt.Rows)
                        {
                            returnList.Add(new Model.TStore() { PKID=int.Parse(dr["PKID"].ToString()),StoreName=dr["StoreName"].ToString()});
                        }
                        return returnList;
                    }
                    else { return null; }

                }
                catch (Exception) { return null; }
                
            }

            public List<Model.TStore> TStore(string sqlCon, int pkid)
            {

                try
                {
                    SqlParameter[] parameter = { new SqlParameter("@PKID", SqlDbType.Int) };
                    parameter[0].Value = pkid;

                    DataTable dt= SqlHelper.ExecuteDataTable(DataProtector.DecryptString(sqlCon), CommandType.StoredProcedure, "ProTStoreSelectByID", parameter);
                    if (dt != null)
                    {
                        var returnList = new List<Model.TStore>();
                        foreach (DataRow dr in dt.Rows)
                        {
                            returnList.Add(new Model.TStore() { PKID = int.Parse(dr["PKID"].ToString()), StoreName = dr["StoreName"].ToString(),Address=dr["Address"].ToString() });
                        }
                        return returnList;
                    }
                    else { return null; }

                }
                catch (Exception) { return null; }

            }

            public Model.TStore TStore(string sqlCon, string serialNumber)
            {
                try
                {
                    SqlParameter[] parameter = { new SqlParameter("@SerialNumber", SqlDbType.NVarChar, 50) };
                    parameter[0].Value = serialNumber;

                    DataTable dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(sqlCon), CommandType.StoredProcedure, "ProTStoreBySerialNumber", parameter);
                    if (dt != null)
                    {
                        return new Model.TStore() { PKID = int.Parse(dt.Rows[0]["PKID"].ToString()), Address = dt.Rows[0]["Address"].ToString(), City = dt.Rows[0]["City"].ToString(), StoreName = dt.Rows[0]["StoreName"].ToString(), Phone = dt.Rows[0]["Phone"].ToString() };
                    }
                    else
                    {
                        return null;
                    }
                }
                catch (Exception) { return null; }
            }
        }
        public class DALTProductCode
        {
            public Model.TProductCode TProductCode(string sqlCon, string serialNumber)
            {
                try
                {
                    SqlParameter[] parameter = { new SqlParameter("@SerialNumber", SqlDbType.NVarChar, 50) };
                    parameter[0].Value = serialNumber;

                    DataTable dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(sqlCon), CommandType.StoredProcedure, "ProTProductCodeExistsBySerialNumber", parameter);
                    if (dt != null)
                    {
                        var model=new Model.TProductCode();
                        foreach (DataRow dr in dt.Rows)
                        {
                            model.PKID = int.Parse(dr["PKID"].ToString());
                            model.SerialNumber = dr["SerialNumber"].ToString();
                            model.Checked = Convert.ToBoolean(dr["Checked"]);
                        }
                        return model;
                    }
                    else
                    {
                        return null;
                    }
                }
                catch (Exception)
                {
                    return null;
                }
            }

            public bool TProductCodeUpdate(string sqlCon, string serialNumber)
            {
                try
                {
                    SqlParameter[] parameter = { new SqlParameter("@SerialNumber", SqlDbType.NVarChar, 50) };
                    parameter[0].Value = serialNumber;

                    int i = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(sqlCon), CommandType.StoredProcedure, "ProTProductCodeUpdateBySerialNumber", parameter);
                    if (i > 0) return true; else return false;
                }
                catch (Exception)
                {
                    return false;
                }
            }

        }

    }
}
